package com.bjsc.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.bjsc.entity.Movie;
import com.bjsc.mapper.MovieMapper;
import com.bjsc.service.IMovieService;
import com.bjsc.storage.Storage;
import com.bjsc.storage.StorageFile;
import org.apache.log4j.LogManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.OutputStream;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author create by VIC.Huang
 * @date 2018/4/10
 */
@Service
public class MovieServiceImpl extends ServiceImpl<MovieMapper, Movie> implements IMovieService {
    @Autowired
    private Storage storage;

    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    private static final String MP4_SUBFIX = ".mp4";

    @Override
    public boolean updateMovie(String title, MultipartFile headImg, MultipartFile movie) {
        if (!headImg.isEmpty() && !movie.isEmpty()) {
            if (!MP4_SUBFIX.equalsIgnoreCase(movie.getOriginalFilename().substring(movie.getOriginalFilename().lastIndexOf(".")))) {
                throw new RuntimeException("请上传mp4格式的视频文件！");
            }

            try {
                byte[] headImgByte = headImg.getBytes();
                StorageFile headImgFile = storage.createFileWithName(
                        sdf.format(new Date()),
                        storage.getNewFileName(),
                        headImg.getContentType(),
                        headImg.getOriginalFilename().substring(headImg.getOriginalFilename().lastIndexOf(".")));
                OutputStream headImgOut = headImgFile.getOutputStream();
                headImgOut.write(headImgByte);
                headImgOut.close();
                String headImgPath = headImgFile.getFullName();

                byte[] movieByte = movie.getBytes();
                StorageFile movieFile = storage.createFileWithName(
                        sdf.format(new Date()),
                        storage.getNewFileName(),
                        headImg.getContentType(),
                        MP4_SUBFIX);
                OutputStream movieOut = movieFile.getOutputStream();
                movieOut.write(movieByte);
                movieOut.close();
                String moviePath = movieFile.getFullName();

                Movie movieEntity = new Movie();
                movieEntity.setTitle(title).setFilePath(moviePath).setHeadImgPath(headImgPath);
                return baseMapper.insert(movieEntity) > 0;

            } catch (IOException e) {
                LogManager.getLogger(MovieServiceImpl.class).error("Failed to save movie!", e);
                return false;
            }
        } else {
            throw new RuntimeException("封面图及视频均不允许为空！");
        }
    }

    @Override
    public boolean deleteById(Serializable id) {
        Movie movie = selectById(id);
        if(null != movie) {
            if (super.deleteById(id)) {
                try {
                    // 删除ftp文件
                    storage.deleteFile(movie.getHeadImgPath());
                    storage.deleteFile(movie.getFilePath());
                } catch (IOException e) {
                    LogManager.getLogger(MovieServiceImpl.class).error("Failed to delete Ftp File! ", e);
                }
                return true;
            }
        }
        return false;
    }
}
